package com.harmonyos.service.impl;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.harmonyos.domain.TbFourm;
import com.harmonyos.domain.TbUser;
import com.harmonyos.service.TbUserService;
import com.harmonyos.mapper.TbUserMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 */
@Service
public class TbUserServiceImpl extends ServiceImpl<TbUserMapper, TbUser>
implements TbUserService{

    @Resource
    private TbUserMapper tbUserMapper;
    @Override
    public String insert(TbUser user) {
        HashMap<String, Object> objectObjectHashMap = new HashMap<>();
        try {
            tbUserMapper.insert(user);
            objectObjectHashMap.put("code","200");
        } catch (Exception e) {
            objectObjectHashMap.put("code","500");
            e.printStackTrace();
        }
        return JSONUtil.toJsonStr(objectObjectHashMap);
    }

    @Override
    public String select(String user) {
        HashMap<String, Object> objectObjectHashMap = new HashMap<>();
        try {
            TbUser user1 = JSONUtil.toBean(user, TbUser.class);
            TbUser tbUser = tbUserMapper.selectById(user1.getUser());
            objectObjectHashMap.put("data",tbUser);
            objectObjectHashMap.put("code","200");
        } catch (Exception e) {
            objectObjectHashMap.put("code","500");
            e.printStackTrace();
        }
        return JSONUtil.toJsonStr(objectObjectHashMap);

    }

    @Override
    public String update(TbUser user) {
        HashMap<String, Object> objectObjectHashMap = new HashMap<>();
        try {
           tbUserMapper.updateById(user);

            objectObjectHashMap.put("code","200");
        } catch (Exception e) {
            objectObjectHashMap.put("code","500");
            e.printStackTrace();
        }
        return JSONUtil.toJsonStr(objectObjectHashMap);
    }

    @Override
    public String selectAll() {
        Map<String, Object> objectObjectHashMap = new HashMap<>();
        try {
            LambdaQueryWrapper<TbUser> mapper = new LambdaQueryWrapper<>();
            List<TbUser> tbUsers = tbUserMapper.selectList(mapper);
            objectObjectHashMap.put("data",tbUsers);
            objectObjectHashMap.put("code","200");
        } catch (Exception e) {
            objectObjectHashMap.put("code","500");
            e.printStackTrace();
        }
        return JSONUtil.toJsonStr(objectObjectHashMap);
    }
}




